package com.hbnu.service.impl;

import com.hbnu.pojo.DormBuild;
import com.hbnu.service.DormManagerService;
import com.hbnu.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chendikai
 * @date 2024-11-13 下午 3:15
 */
public class DormManagerServiceImpl implements DormManagerService {
    @Override
    public List<DormBuild> find() {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtils.getConnection();
            String sql = "select * from tb_dormbuild";

            preparedStatement = connection.prepareStatement(sql);

            resultSet = preparedStatement.executeQuery();

            List<DormBuild> builds = new ArrayList<DormBuild>();

            while (resultSet.next()) {
                DormBuild build = new DormBuild();
                build.setId(resultSet.getInt("id"));
                build.setName(resultSet.getString("name"));
                build.setDisabled(resultSet.getInt("disabled"));
                build.setRemark(resultSet.getString("remark"));

                builds.add(build);
            }
            return builds;


        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.close(resultSet, preparedStatement, connection);
        }
    }
}
